package etimer

import "time"

type Timer struct {
	now      time.Time
	interval float64 //间隔秒
}

func NewTimer(seconds float64) *Timer {
	t := new(Timer)
	t.interval = seconds
	t.now = time.Now()
	return t
}

func (this *Timer) Reset() {
	this.now = time.Now()
}

func (this *Timer) Over() bool {
	if time.Since(this.now).Seconds() > this.interval {
		this.Reset()
		return true
	}
	return false
}

type ITimer struct {
	interval int64
	last     int64
}

func NewITimer(seconds int64) *ITimer {
	t := new(ITimer)
	t.interval = seconds
	t.Reset()
	return t
}

func (this *ITimer) Reset() {
	this.last = time.Now().Unix()
	this.last -= this.last % this.interval
}

func (this *ITimer) Over() bool {
	if (time.Now().Unix() - this.last) >= this.interval {
		this.Reset()
		return true
	}
	return false
}
